package com.example.administrator.wallet.view.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.ActivityUtils;
import com.example.administrator.wallet.MainActivity;
import com.example.administrator.wallet.R;
import com.example.administrator.wallet.base.BaseActivity;
import com.example.administrator.wallet.utils.SharedPrefsUtil;
import com.example.mylibrary.src.main.java.com.quincysx.crypto.utils.Base58;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

public class FirstPswActivity extends BaseActivity {

    @Bind(R.id.finish_img)
    ImageButton finishImg;
    @Bind(R.id.view_title)
    TextView viewTitle;
    @Bind(R.id.ed1)
    EditText ed1;
    @Bind(R.id.ed2)
    EditText ed2;
    @Bind(R.id.ed3)
    EditText ed3;
    @Bind(R.id.ed4)
    EditText ed4;
    @Bind(R.id.ed5)
    EditText ed5;
    @Bind(R.id.ed6)
    EditText ed6;
    @Bind(R.id.activity_first_psw)
    LinearLayout activityFirstPsw;
    @Bind(R.id.psw_name)
    TextView pswName;
    @Bind(R.id.edit)
    LinearLayout edit_linear;
    private int FIRST_IN = 1;
    private int TWO_IN = 2;
    private int THREE_IN = 3;
    private List<EditText> ed_list = new ArrayList<>();
    private String datas = "";
    private String datas2 = "";
    private SharedPreferences wallet;
    private int count;
    private SharedPreferences.Editor edit;
    private String data;
    public static String password = "";
    private int code=0;
    @Override
    public void onResume() {
        super.onResume();
        ed1.setText("");
        initListEd();
        hideSelected(ed_list);
        init();
    }

    @Override
    public void init() {
        SharedPrefsUtil.putValue(this,"wallet","codes",code);
        wallet = getSharedPreferences("wallet", MODE_PRIVATE);
        count = wallet.getInt("count", FIRST_IN);
        Log.e("TAG", count + "");
        edit = wallet.edit();
        if (count == FIRST_IN) {
            pswName.setText(R.string.set_pst);
        } else if (count == THREE_IN) {
            pswName.setText(R.string.put_pst);
        }
        setStatuColor();
        finishImg.setVisibility(View.VISIBLE);
        viewTitle.setText(R.string.set_yPst);
        ed2.clearFocus();
        ed2.setInputType(InputType.TYPE_NULL);
        ed3.clearFocus();
        ed3.setInputType(InputType.TYPE_NULL);
        ed4.clearFocus();
        ed4.setInputType(InputType.TYPE_NULL);
        ed5.clearFocus();
        ed5.setInputType(InputType.TYPE_NULL);
        ed6.clearFocus();
        ed6.setInputType(InputType.TYPE_NULL);
        showSoftInputFromWindow(this, ed1);
        ed1.setImeOptions(EditorInfo.IME_ACTION_SEND);
        setEdStatu(ed1);
        initListEd();
        ed1.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                //这里注意要作判断处理，ActionDown、ActionUp都会回调到这里，不作处理的话就会调用两次
                if (KeyEvent.KEYCODE_ENTER == keyCode && KeyEvent.ACTION_DOWN == event.getAction()) {

                    return true;
                }
                return false;
            }
        });
    }

    private void initListEd() {
        ed_list.clear();
        ed_list.add(ed1);
        ed_list.add(ed2);
        ed_list.add(ed3);
        ed_list.add(ed4);
        ed_list.add(ed5);
        ed_list.add(ed6);
    }

    private void setEdStatu(final EditText ed) {
        ed.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                if ((ed1.getText().toString().trim()).equals("") || ed1.getText().toString() == null) {
                    initListEd();
                    hideSelected(ed_list);
                } else {
                    switch (ed1.getText().length()) {

                        case 1:
                            initListEd();
                            ed_list.remove(ed1);
                            hideSelected(ed_list);
                            ed1.setSelected(true);
                            break;
                        case 2:
                            initListEd();
                            ed_list.remove(ed1);
                            ed_list.remove(ed2);
                            hideSelected(ed_list);
                            ed1.setSelected(true);
                            ed2.setSelected(true);
                            break;
                        case 3:
                            initListEd();
                            ed_list.remove(ed1);
                            ed_list.remove(ed2);
                            ed_list.remove(ed3);
                            hideSelected(ed_list);
                            ed1.setSelected(true);
                            ed2.setSelected(true);
                            ed3.setSelected(true);
                            break;
                        case 4:
                            initListEd();
                            ed_list.remove(ed1);
                            ed_list.remove(ed2);
                            ed_list.remove(ed3);
                            ed_list.remove(ed4);
                            hideSelected(ed_list);
                            ed1.setSelected(true);
                            ed2.setSelected(true);
                            ed3.setSelected(true);
                            ed4.setSelected(true);
                            break;
                        case 5:
                            initListEd();
                            ed_list.remove(ed1);
                            ed_list.remove(ed2);
                            ed_list.remove(ed3);
                            ed_list.remove(ed4);
                            ed_list.remove(ed5);
                            hideSelected(ed_list);
                            ed1.setSelected(true);
                            ed2.setSelected(true);
                            ed3.setSelected(true);
                            ed4.setSelected(true);
                            ed5.setSelected(true);
                            break;
                        case 6:
                            initListEd();
                            ed_list.remove(ed1);
                            ed_list.remove(ed2);
                            ed_list.remove(ed3);
                            ed_list.remove(ed4);
                            ed_list.remove(ed5);
                            ed_list.remove(ed6);
                            hideSelected(ed_list);
                            ed1.setSelected(true);
                            ed2.setSelected(true);
                            ed3.setSelected(true);
                            ed4.setSelected(true);
                            ed5.setSelected(true);
                            ed6.setSelected(true);
                            //处理事件
                            if (ed6.isSelected()) {
                                if (count == FIRST_IN) {
                                    if (ed6.isSelected()) {
                                        SharedPrefsUtil.putValue(FirstPswActivity.this, "wallet", "count", TWO_IN);
                                        SharedPrefsUtil.putValue(FirstPswActivity.this, "wallet", "datas", charSequence.toString());
                                        ed1.setText("");
                                        initListEd();
                                        hideSelected(ed_list);
                                        init();
                                        pswName.setText(R.string.Again_pst);
                                    } else {
                                        Toast.makeText(FirstPswActivity.this, R.string.pst_type, Toast.LENGTH_SHORT).show();
                                    }
                                } else if (count == TWO_IN) {
                                    data = wallet.getString("datas", "");
                                    datas2 = charSequence.toString();
                                    SharedPrefsUtil.putValue(FirstPswActivity.this, "wallet", "count", FIRST_IN);
                                    if (data.equals(datas2)) {
                                        String encode = Base58.encode(data.getBytes());
                                        Intent intent = new Intent(FirstPswActivity.this, BackupsActivity.class);
                                        intent.putExtra("log", encode);
                                        startActivity(intent);
                                    } else {
                                        ed1.setText("");
                                        initListEd();
                                        hideSelected(ed_list);
                                        init();
                                        Toast.makeText(FirstPswActivity.this, R.string.two_pst_error, Toast.LENGTH_SHORT).show();
                                    }
                                }
                                if (count == THREE_IN) {
                                    String log = wallet.getString("log", "123");
                                    String s = ed1.getText().toString();
                                    if (ed6.isSelected() && log.equals(Base58.encode(s.getBytes()))) {
                                        ActivityUtils.startActivity(FirstPswActivity.this, MainActivity.class);
                                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                                        imm.hideSoftInputFromWindow(ed1.getWindowToken(), 0);
                                        finish();
                                    } else {
                                        Toast.makeText(FirstPswActivity.this, R.string.pst_error, Toast.LENGTH_SHORT).show();
                                    }
                                }
                            }

                            break;
                    }
                }
                if (count == 1) {
                    datas = ed1.getText().toString();
                    Log.e("TAG----1", datas);
                } else {
                    Log.e("TAG----2", datas2);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    public void hideSelected(List<EditText> list_ed) {
        for (int i = 0; i < list_ed.size(); i++) {
            list_ed.get(i).setSelected(false);
        }
    }

    @Override
    public int getLayoutResource() {
        return R.layout.activity_first_psw;
    }

    public static void showSoftInputFromWindow(Activity activity, EditText editText) {
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(editText, 0);
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }


    @OnClick({R.id.finish_img, R.id.edit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.finish_img:
                finish();
                break;
            case R.id.edit:
                showSoftInputFromWindow(FirstPswActivity.this, ed1);
                ed1.setImeOptions(EditorInfo.IME_ACTION_SEND);
                break;
        }
    }
}
